﻿//-----------------------------------------------------------------------
// <copyright file="UnregisteredUserData.xaml.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BookCaching.Settings;

namespace BookCaching.CustomControls
{
    /// <summary>
    /// Custom control displaying data if no user is logged in.
    /// </summary>
    public partial class UnregisteredUserData : UserControl
    {
        public UnregisteredUserData()
        {
            this.InitializeComponent();
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            string test = "test";
            byte[] saltByte = Encoding.UTF8.GetBytes("somesalt");

            string passwordEncrypted;

            using (Aes aes = new AesManaged())
            {
                Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(test, saltByte);
                aes.Key = deriveBytes.GetBytes(128 / 8);
                aes.IV = aes.Key;
                using (MemoryStream encryptionStream = new MemoryStream())
                {
                    using (CryptoStream encrypt = new CryptoStream(encryptionStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(this.passwordText.Password);
                        encrypt.Write(passwordBytes, 0, passwordBytes.Length);
                        encrypt.FlushFinalBlock();
                    }
                    passwordEncrypted = Convert.ToBase64String(encryptionStream.ToArray());
                }
            }

            Knihotoc_WCFService.Knihotoc_WP7ServiceClient client = new Knihotoc_WCFService.Knihotoc_WP7ServiceClient();
            client.LoginUserCompleted += Knihotoc_WP7ServiceClient_LoginUserCompleted;
            client.LoginUserAsync(this.usernameText.Text, this.passwordText.Password);

            //using (Aes aes = new AesManaged())
            //{
            //    Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(test, saltByte);
            //    aes.Key = deriveBytes.GetBytes(128 / 8);
            //    aes.IV = aes.Key;
            //    using (MemoryStream stream = new MemoryStream())
            //    {
            //        using (CryptoStream encrypt = new CryptoStream(stream, aes.CreateDecryptor(), CryptoStreamMode.Write))
            //        {
            //            byte[] utfD1 = Convert.FromBase64String(final);
            //            encrypt.Write(utfD1, 0, utfD1.Length);
            //            encrypt.Flush();
            //        }
            //        byte[] decryptedData = stream.ToArray();
            //        final = UTF8Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
            //    }
            //}            
        }

        void Knihotoc_WP7ServiceClient_LoginUserCompleted(object sender, Knihotoc_WCFService.LoginUserCompletedEventArgs e)
        {
            if (e.Error != null || e.Result == null)
            {
                return;
            }

            CurrentSettings.Instance.User = new Data.User(e.Result);
            if (this.UserLoggedIn != null)
            {
                this.UserLoggedIn(this, null);
            }
        }

        public event EventHandler UserLoggedIn;

        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ForgottenPasswordButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
